/*
 * Created on 2005-9-9
 *
 */
package com.hd123.rumba.request;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.hibernate.HibernateException;

import org.apache.axis.AxisEngine;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.transport.http.AxisServlet;
import org.apache.axis.utils.XMLUtils;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;

import com.hd123.rumba.runtime.Configuration;
import com.hd123.rumba.runtime.HibernateSession;
import com.hd123.rumba.user.IUser;
import com.hd123.rumba.util.ObjectUtil;
import com.hd123.rumba.util.StopWatch;

/**
 * @author caili
 */
public class AxisRequestHandler extends AxisServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    process("get", request, response);
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    process("post", request, response);
  }

  protected void process(String method, HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    StopWatch sw = new StopWatch().start();
    String remoteHost = request.getRemoteHost();
    String requestURI = request.getRequestURI();
    String queryString = request.getQueryString();
    HttpSession session = request.getSession();
    String sessionId = session == null ? null : session.getId();
    String userId = null;

    AxisSession.setSession(session);
    try {

      try {
        IUser user = AxisSession.getCurrentUser();
        if (user != null)
          userId = user.getFullLogin();
      } catch (Exception e) {
      }

      try {
        if ("get".equals(method))
          super.doGet(request, response);
        else if ("post".equals(method))
          super.doPost(request, response);
      } catch (Exception e) {
        Logger.getLogger(this.getClass()).error(ObjectUtil.getErrorTrace(e));
      }
    } finally {
      AxisSession.removeSession();
      try {
        HibernateSession.closeSession();
      } catch (HibernateException ex) {
        throw AxisFault.makeFault(ex);
      }
    }

    Logger.getLogger(this.getClass()).info(
        "perf|" + sw.stop() + "|" + remoteHost + ": " + method + " " + requestURI + "?"
            + queryString + " !@# " + sessionId + "|" + userId);
  }

  public void init() throws ServletException {
    super.init();

    try {
      List<String> wsddFiles = Configuration.getInstance().getWsddList();
      if (wsddFiles.size() > 0) {
        // File dir = new
        // File(getServletContext().getRealPath("/WEB-INF/classes"));
        AxisEngine engine = getEngine(this);
        MessageContext msgContext = new MessageContext(engine);
        for (int i = 0; i < wsddFiles.size(); ++i) {
          String rn = wsddFiles.get(i);
          // File file = new File(dir, rn);
          Logger.getLogger(this.getClass()).info("部署WEB服务: " + rn);
          // Document doc = XMLUtils.newDocument(new FileInputStream(file));
          Document doc = XMLUtils.newDocument(this.getClass().getResourceAsStream(rn));
          Document result = new org.apache.axis.utils.Admin().process(msgContext, doc
              .getDocumentElement());
          if (result != null) {
            System.out.println(XMLUtils.DocumentToString(result));
          }
        }
      }
    } catch (Exception e) {
      Logger.getLogger(getClass()).error(e.getMessage());
      throw new ServletException(e);
    }
  }
}
